package org.jeecg.modules.demo.traderquota.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.traderquota.entity.PostorderPrice;
import org.jeecg.modules.demo.traderquota.entity.TraderQ;
import org.jeecg.modules.demo.traderquota.entity.TraderQuota;
import org.jeecg.modules.demo.traderquota.service.ITraderQService;
import org.jeecg.modules.demo.traderquota.service.ITraderQuotaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 额度
 */
@Api(tags="traderQ")
@RestController
@RequestMapping("/traderQ/traderQ")
@Slf4j
public class TraderQController {

    @Autowired
    private ITraderQService traderQService;
    @Autowired
    private ITraderQuotaService traderQuotaService;

    /**
     * 分页列表查询
     *
     * @param traderQ
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "测试-分页列表查询")
    @Operation(summary="测试-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<TraderQ>> queryPageList(TraderQ traderQ,
                                                @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                HttpServletRequest req) {
        QueryWrapper<TraderQ> queryWrapper = QueryGenerator.initQueryWrapper(traderQ, req.getParameterMap());
        queryWrapper.eq("INOUTSIDE",2);
        Page<TraderQ> page = new Page<TraderQ>(pageNo, pageSize);
        IPage<TraderQ> pageList = traderQService.page(page, queryWrapper);

        List<TraderQ> traderQList = pageList.getRecords();
        List<TraderQ> traderQs = new ArrayList<>();
        for(TraderQ t:traderQList){

            List<PostorderPrice> postorderPricesList = traderQService.countTotal(t.getTraderid());
            Double quotaUsed = 0.0;//总使用额度
            for (PostorderPrice pp:postorderPricesList){
                Double total = 0.0;
                Double price = pp.getPOPRICE();//挂单价格
                Double num = pp.getPOQUANTITY();//挂单数量
                Integer day = (int)Math.round((Double.valueOf(pp.getTIANSHU()))/365);//挂单时长  天数/365天四舍五入计算年
                total = price * num * day;//挂单总价

                quotaUsed += total;
            }
            Double quotaTotal = 0.0;
            if(t.getQuotaTotal() != null){
                quotaTotal = t.getQuotaTotal();
            }
            t.setQuotaRemaining(quotaTotal - quotaUsed);
            traderQs.add(t);
        }
        pageList.setRecords(traderQs);

        return Result.OK(pageList);
    }


    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "额度-通过主表ID查询")
    @Operation(summary="额度-通过主表ID查询")
    @GetMapping(value = "/queryTraderQuotaByMainId")
    public Result<IPage<TraderQuota>> queryTraderQuotaListByMainId(@RequestParam(name="id",required=true) String id) {
        List<TraderQuota> traderQuotaList = traderQuotaService.selectByMainId(id);
        IPage <TraderQuota> page = new Page<>();
        page.setRecords(traderQuotaList);
        page.setTotal(traderQuotaList.size());

        return Result.OK(page);
    }


    /**
     *   添加
     *
     * @param traderQuota
     * @return
     */
    @AutoLog(value = "traderQuota-添加")
    @ApiOperation(value="traderQuota-添加", notes="traderQuota-添加")
//    @RequiresPermissions("traderQuota:traderQuota:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody TraderQuota traderQuota) {

        String id = String.valueOf(IdWorker.getId());
        traderQuota.setId(id);

        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        traderQuota.setCreateBy(sysUser.getUsername());

//        long time = System.currentTimeMillis();
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd:HH-mm-ss");
//        String day = dateFormat.format(time);
        traderQuota.setCreateTime(new Date());

        Double quota = traderQuotaService.saveQuota(traderQuota);
        traderQService.updateTraderQById(traderQuota.getTraderid(),quota);

        return Result.OK("添加成功！");
    }



}
